#!/bin/sh
# 定义 程序目录变量
BASE_DIR="${BASE_PATH}/${SYSTMED_NAME}"
# 进入工作目录
cd ${WORKSPACE}/${SYSTMED_NAME}/target/;
echo $TARGET_ARN
# 健康检查
function health() {
    if [ $1 -eq 200 ];then
        echo "health_code:$CODE"
        echo "$HEALTH"
        echo "SUCCESS"
    else
        echo "health_code:$CODE"
        echo "$HEALTH"
        echo "FAILURE"
        exit 1
    fi
}

# 全量发布
if ${complete} 
then
    if [ "$mode" == "手动" ] 
    then
        echo "手动"
        instanceID=`echo ${setupnode}|awk -F',' '{print $1}'`
        ssh_port=`echo ${setupnode}|awk -F',' '{print $2}'`
        service_port=`echo ${setupnode}|awk -F',' '{print $3}'`
        echo "开始$ssh_port"
        echo $instanceID
        ssh -tt -p $JUMP_SSH_PORT $GLOBAL_IP "
            /var/anaconda3/bin/python /data/awsapi/deregister-targets.py $TARGET_ARN $instanceID $service_port;
        " 
        echo "$PRE_EIB_TIME后开始"
        sleep $PRE_EIB_TIME;
        ssh -p $ssh_port -o StrictHostKeyChecking=no root@$GLOBAL_IP "systemctl stop ${SYSTMED_NAME}.service;"
        echo "等待$SERVICE_STOP_TIME"
        sleep $SERVICE_STOP_TIME
        ssh -p $ssh_port root@$GLOBAL_IP "ss -tnl | grep $service_port"
        ssh -p $ssh_port root@$GLOBAL_IP "rm -rf $BASE_DIR/; mkdir -p $BASE_DIR/"
        scp -P $ssh_port ${SYSTMED_NAME}-${version}.tar.gz  root@$GLOBAL_IP:$BASE_DIR/
        ssh -p $ssh_port root@$GLOBAL_IP "cd $BASE_DIR; tar -zxvf ./${SYSTMED_NAME}-${version}.tar.gz"
        ssh -p $ssh_port root@$GLOBAL_IP "mkdir -p $BASE_DIR/config"
        ssh -p $ssh_port root@$GLOBAL_IP "
            if [ ! -d '/lib/systemd/system/${SYSTMED_NAME}.service' ]; then
                sed -i 's#softpath#$BASE_DIR/bin#g' $BASE_DIR/bin/soft-service.sh;
                sed -i 's#soft-service#${SYSTMED_NAME}#g' $BASE_DIR/bin/soft-service.sh; 
                cp -a $BASE_DIR/bin/soft-service.sh /lib/systemd/system/${SYSTMED_NAME}.service;
                systemctl daemon-reload; 
                systemctl start ${SYSTMED_NAME}.service; 
            else
                systemctl daemon-reload; 
                systemctl start ${SYSTMED_NAME}.service;
            fi
        "
        echo "等待$SERVICE_START_TIME"
        sleep $SERVICE_START_TIME
        ssh -p $ssh_port root@$GLOBAL_IP "ss -tnl | grep $service_port"
        HEALTH=`ssh -tt -p $ssh_port $GLOBAL_IP "curl -I http://127.0.0.1:$service_port/health" |grep HTTP`
        CODE=`echo $HEALTH|awk '{print $2}'`
        health $CODE
        ssh -tt -p $JUMP_SSH_PORT $GLOBAL_IP "
            /var/anaconda3/bin/python /data/awsapi/register-targets.py $TARGET_ARN $instanceID $service_port
        "
        echo "等待$POST_EIB_TIME"
        sleep $POST_EIB_TIME;
    else
        echo "自动"  
        for node in ${list_node}
        do
            instanceID=`echo $node|awk -F',' '{print $1}'`
            ssh_port=`echo $node|awk -F',' '{print $2}'`
            service_port=`echo $node|awk -F',' '{print $3}'`

            echo "开始$ssh_port"
            echo $instanceID
            ssh -tt -p $JUMP_SSH_PORT $GLOBAL_IP "
                /var/anaconda3/bin/python /data/awsapi/deregister-targets.py $TARGET_ARN $instanceID $service_port;
            " 
            echo "$PRE_EIB_TIME后开始"
       	    sleep $PRE_EIB_TIME
            ssh -p $ssh_port -o StrictHostKeyChecking=no root@$GLOBAL_IP "systemctl stop ${SYSTMED_NAME}.service;"
            echo "等待$SERVICE_STOP_TIME"
            sleep $SERVICE_STOP_TIME
            ssh -p $ssh_port root@$GLOBAL_IP "ss -tnl | grep $service_port"
            ssh -p $ssh_port root@$GLOBAL_IP "rm -rf $BASE_DIR/; mkdir -p $BASE_DIR/"
            scp -P $ssh_port ${SYSTMED_NAME}-${version}.tar.gz  root@$GLOBAL_IP:$BASE_DIR/
            ssh -p $ssh_port root@$GLOBAL_IP "cd $BASE_DIR; tar -zxvf ./${SYSTMED_NAME}-${version}.tar.gz"
            ssh -p $ssh_port root@$GLOBAL_IP "mkdir -p $BASE_DIR/config"
            ssh -p $ssh_port root@$GLOBAL_IP "
                if [ ! -d '/lib/systemd/system/${SYSTMED_NAME}.service' ]; then
                    sed -i 's#softpath#$BASE_DIR/bin#g' $BASE_DIR/bin/soft-service.sh;
                	sed -i 's#soft-service#${SYSTMED_NAME}#g' $BASE_DIR/bin/soft-service.sh; 
                	cp -a $BASE_DIR/bin/soft-service.sh /lib/systemd/system/${SYSTMED_NAME}.service;
                    systemctl daemon-reload; 
                    systemctl start ${SYSTMED_NAME}.service; 
                    systemctl enable ${SYSTMED_NAME}.service;
                else
                    systemctl daemon-reload; 
                    systemctl start ${SYSTMED_NAME}.service; 
                fi
            "
            echo "等待$SERVICE_START_TIME"
            sleep $SERVICE_START_TIME
            ssh -p $ssh_port root@$GLOBAL_IP "ss -tnl | grep $service_port"
            HEALTH=`ssh -tt -p $ssh_port $GLOBAL_IP "curl -I http://127.0.0.1:$service_port/health" |grep HTTP`
            CODE=`echo $HEALTH|awk '{print $2}'`
            health $CODE
            ssh -tt -p $JUMP_SSH_PORT $GLOBAL_IP "
                /var/anaconda3/bin/python /data/awsapi/register-targets.py $TARGET_ARN $instanceID $service_port;
            " 
            echo "等待$POST_EIB_TIME"
            sleep $POST_EIB_TIME;
            echo "结束$ssh_port"
            echo
        done
    fi

# 非全量发布
else
    if [ "$mode" == "手动" ] 
    then
        echo "手动"
        instanceID=`echo ${setupnode}|awk -F',' '{print $1}'`
        ssh_port=`echo ${setupnode}|awk -F',' '{print $2}'`
        service_port=`echo ${setupnode}|awk -F',' '{print $3}'`
        echo "开始$ssh_port"
        echo $instanceID
        ssh -tt -p $JUMP_SSH_PORT $GLOBAL_IP "
            /var/anaconda3/bin/python /data/awsapi/deregister-targets.py $TARGET_ARN $instanceID $service_port;
        " 
        echo "$PRE_EIB_TIME后开始"
        sleep $PRE_EIB_TIME;
        ssh -p $ssh_port root@$GLOBAL_IP "systemctl stop ${SYSTMED_NAME}.service;"
        echo "等待$SERVICE_STOP_TIME"
        sleep $SERVICE_STOP_TIME
        ssh -p $ssh_port root@$GLOBAL_IP "ss -tnl | grep $service_port"
        ssh  -p $ssh_port root@$GLOBAL_IP "rm -rf $BASE_DIR/lib/${SYSTMED_NAME}-${version}.jar"
        scp  -P $ssh_port ${SYSTMED_NAME}-${version}.jar  root@$GLOBAL_IP:$BASE_DIR/lib/
        ssh  -p $ssh_port root@$GLOBAL_IP "systemctl start ${SYSTMED_NAME}.service;"
        echo "等待$SERVICE_START_TIME"
        sleep $SERVICE_START_TIME
        ssh -p $ssh_port root@$GLOBAL_IP "ss -tnl | grep $service_port"
        HEALTH=`ssh -tt -p $ssh_port $GLOBAL_IP "curl -I http://127.0.0.1:$service_port/health" |grep HTTP`
        CODE=`echo $HEALTH|awk '{print $2}'`
        health $CODE
        ssh -tt -p $JUMP_SSH_PORT $GLOBAL_IP "
            /var/anaconda3/bin/python /data/awsapi/register-targets.py $TARGET_ARN $instanceID $service_port;
        "
        echo "等待$POST_EIB_TIME"
        sleep $POST_EIB_TIME;
    else
        echo "自动"
        for node in ${list_node}
        do
            instanceID=`echo $node|awk -F',' '{print $1}'`
            ssh_port=`echo $node|awk -F',' '{print $2}'`
            service_port=`echo $node|awk -F',' '{print $3}'`

            echo "开始$ssh_port"
            echo $instanceID
            ssh -tt -p $JUMP_SSH_PORT $GLOBAL_IP "
                /var/anaconda3/bin/python /data/awsapi/deregister-targets.py $TARGET_ARN $instanceID $service_port;
            " 
            echo "$PRE_EIB_TIME后开始"
       	    sleep $PRE_EIB_TIME
            ssh -p $ssh_port root@$GLOBAL_IP "systemctl stop ${SYSTMED_NAME}.service;"
            echo "等待$SERVICE_STOP_TIME"
            sleep $SERVICE_STOP_TIME
            ssh -p $ssh_port root@$GLOBAL_IP "ss -tnl | grep $service_port"
            ssh  -p $ssh_port root@$GLOBAL_IP "rm -rf $BASE_DIR/lib/${SYSTMED_NAME}-${version}.jar"
            scp  -P $ssh_port ${SYSTMED_NAME}-${version}.jar  root@$GLOBAL_IP:$BASE_DIR/lib/
            ssh  -p $ssh_port root@$GLOBAL_IP "systemctl start ${SYSTMED_NAME}.service;"
            echo "等待$SERVICE_START_TIME"
            sleep $SERVICE_START_TIME
            ssh -p $ssh_port root@$GLOBAL_IP "ss -tnl | grep $service_port"
            HEALTH=`ssh -tt -p $ssh_port $GLOBAL_IP "curl -I http://127.0.0.1:$service_port/health" |grep HTTP`
            CODE=`echo $HEALTH|awk '{print $2}'`
            health $CODE
            ssh -tt -p $JUMP_SSH_PORT $GLOBAL_IP "
                /var/anaconda3/bin/python /data/awsapi/register-targets.py $TARGET_ARN $instanceID $service_port;
            "
            echo "等待$POST_EIB_TIME"
            sleep $POST_EIB_TIME;
            echo "结束$ssh_port"
            echo
        done
    fi
fi